import axios from 'axios'
import { Notification,MessageBox } from 'element-ui'
import Config from '@/settings'
import GLOBAL from '@/common/global_variable.js'
import Bus from '@/common/bus.js'
var show = false;

// 创建axios实例
const service = axios.create({
  // baseURL: GLOBAL.base_url, // api 的 base_url
  timeout: Config.timeout // 请求超时时间
})

// request拦截器
 service.interceptors.request.use(
  config => {
    if (localStorage.getItem('Authorization')) {
      config.headers['Authorization'] = localStorage.getItem('Authorization')+ ''// 让每个请求携带自定义token 请根据实际情况自行修改
    }
    config.headers['Content-Type'] = 'application/json;UTF-8'
    return config
  },
  error => {
    // Do something with request error
    console.log(error,'// for debug') // for debug
    Promise.reject(error)
  }
)

// response 拦截器
 service.interceptors.response.use(
    function(res) {
        // console.log(res,'res');
        let code = res.data.status || res.data.code;
        let msg = res.data.data || res.data.message  || res.message
        if (code == 403) {//控制多次请求失效中，之弹窗一次
            if (!show) {
                show = true;
                MessageBox.confirm(
                  '登录状态已过期，您可以继续留在该页面，或者重新登录',
                  '系统提示',
                  {
                    confirmButtonText: '重新登录',
                    cancelButtonText: '取消',
                    type: 'warning'
                  }
                  ).then(() => {
                    Bus.$emit('toGoLogin');//token失效跳回登录页s
                    show = false
                }).catch(() => {
                    show = false
                })
            }
            return Promise.reject(msg)
        } else if (code < 200 || code > 300 ) {
		    Notification.error({title: '错误',message: msg ,type:'error'});
		    return Promise.reject(msg)
		} else {
		    return res.data
		}
    },
    function(err) {
        // console.log(err.response,'err');
        let code = 0
        try {
          code = err.response.data.status
          
        } catch (e) {
          if (err.toString().indexOf('Error: timeout') !== -1) {
            Notification.error({title: '错误',message:'网络请求超时!' ,type:'error'});
            return Promise.reject('网络请求超时!')
          }
        }
        if (code) {
          if (code === 401) { //token被清空代表已经退出登录，退出登录后不在触发
              if (!show) {
                  show = true;
                  MessageBox.confirm(
                    '登录状态已过期，您可以继续留在该页面，或者重新登录',
                    '系统提示',
                    {
                      confirmButtonText: '重新登录',
                      cancelButtonText: '取消',
                      type: 'warning'
                    }
                    ).then(() => {
                      Bus.$emit('toGoLogin');//token失效跳回登录页s
                      show = false
                  }).catch(() => {
                      show = false
                  })
              }
          } else {
            const errorMsg = err.response.data.message
            if (errorMsg !== undefined) {
              Notification.error({
                title: errorMsg,
                duration: 5000
              })
            }
          }
        } else {
          Notification.error({
            title: '接口请求失败',
            duration: 5000
          })
        }
        console.log(err) // for debug
        return Promise.reject(err)
    }
)
export default service
